const cloud = require('wx-server-sdk');
const tencentcloud = require("tencentcloud-sdk-nodejs-hunyuan");
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const secret_id = "AKIDTrAyiFbZZQ7cx77znSyQflz6B6sUxEID";
const secret_key = "mMjwwjKixzqUyIHAsX3PZZO2XtAKRN6i";
const HunyuanClient = tencentcloud.hunyuan.v20230901.Client;
const client = new HunyuanClient({
  credential: {
    secretId: secret_id,
    secretKey: secret_key,
  },
  region: "ap-guangzhou",
  profile: {
    httpProfile: {
      endpoint: "hunyuan.tencentcloudapi.com",
    },
  },
});
exports.main = async (event, context) => {
  try {
    const { resultImageFileID } = event;
    if (!resultImageFileID) throw new Error('参数缺失');
    console.log('后端：下载结果图');
    const imageRes = await cloud.downloadFile({ fileID: resultImageFileID });
    const imageBase64 = imageRes.fileContent.toString('base64');
    console.log('后端：Base64 转换完成');
    const prompt = "你是一位深谙东方美学的时尚造型师和文化顾问。眼前是一位顾客试穿我们香云纱服饰的效果图。香云纱是承载着百年非遗技艺的‘软黄金’。 请你基于香云纱独特的植物染色、河泥覆盖工艺所带来的‘越穿越亮’的特性，为这位顾客提供一份专属的造型报告：气质解读：首先，简单分析这件香云纱如何衬托出穿着者的气质，营造了怎样的氛围感？场景融合：为她推荐2-3个最能展现这身搭配魅力的现代生活场景。点睛配饰：推荐一款最能与香云纱产生“灵魂共鸣”的配饰（如包、鞋、首饰），并简要说明理由。鞋子、首饰）、以及色彩搭配这几个方面，给出具体的、专业的建议。请用友好且鼓励的语气回答。";
    console.log('后端：正在调用腾讯混元 vision 模型');
    const params = {
      Model: "hunyuan-vision",
      Messages: [
        {
          Role: "user",
          Contents: [
            { 
              Type: "text", 
              Text: prompt 
            },
            { 
              Type: "image_url",
              ImageUrl: {
                // 将带前缀的 Base64 字符串，伪装成一个 "Data URL"
                Url: "data:image/jpeg;base64," + imageBase64 
              }
            }
          ]
        }
      ]
    };
    const response = await client.ChatCompletions(params);
    const suggestionText = response.Choices[0].Message.Content;
    console.log('后端：成功获取 AI 建议');
    return {
      success: true,
      suggestion: suggestionText
    };
  } catch (error) {
    console.error('后端执行出错:', error);
    return { success: false, error: error.message };
  }
}